# 爬J站
# 爬取所有的图片和title
import requests
from lxml import etree


class Jzhang(object):
    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}

    def parse_url(self, url, data):  # 获取源码
        response = requests.get(url=url, headers=self.headers, data=data, verify=False)
        return response.content.decode()

    def save_sound_code(self, parse_str, num):  # 保存源码
        file_name = "J站第{}页源码".format(num) + str(".html")
        with open(file_name, 'a', encoding="utf-8")as f:
            f.write(parse_str)

    def Img_list(self, parse_str):  # 第一页面下的所有url
        html = etree.HTML(parse_str)
        div_list = html.xpath('//div[@class="thumb pos-r"]/a/@href')
        return div_list

    def gain_img_url(self, url_one):  # 每张图片的地址
        response = requests.get(url=url_one, headers=self.headers).content.decode()
        html_str = etree.HTML(response)
        img_url = html_str.xpath('//div[@id="content-innerText"]/p/img/@src')
        return img_url

    def save_img(self, img, num_img):  # 保存图片
        with open("J站第{}张.jpg".format(num_img), "ab") as f:
            f.write(requests.get(img).content)

    def run(self):
        for page in range(1, 111):
            data = {
                'type': 'index',
                'paged': page
            }
            print("正在下载第{}页面...".format(page))
            url = 'https://www.jdlingyu.mobi/'
            num = 0
            parse_str = self.parse_url(url, data)  # 遍历多个页面
            num += 1
            self.save_sound_code(parse_str, num)  # 保存源码
            url_list = self.Img_list(parse_str)  # 提取第一页面的url
            for url_one in url_list:
                img_list = self.gain_img_url(url_one)  # 遍历第二个页面，提取图片地址
                num_img = 1
                for img in img_list:  # 保存图片地址
                    self.save_img(img, num_img)
                    num_img += 1


if __name__ == '__main__':
    spider = Jzhang()
    spider.run()
